package com.wenjh.jdmzt.android.activity;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.preference.Preference;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wenjh.jdmzt.android.view.ActionBar;
import com.wenjh.jdmzt.service.model.User;
import com.wenjh.jdmzt.util.Log4Android;
import com.wenjh.jdmzt.util.Toaster;

public abstract class BaseFragment extends Fragment {
	private WeakReference<View> contentViewReference = null;
	private Map<Integer, WeakReference<View>> viewFounds = null;
	private ActionBar actionBar = null;
	final protected Log4Android log = new Log4Android(getClass()
			.getSimpleName());
	protected User currentUser = null;
	protected Preference preference = null;
	private boolean created = false;
	

	@Override
	public final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(getLayout(), container, false);
		contentViewReference = new WeakReference<View>(v);
		viewFounds = new HashMap<Integer, WeakReference<View>>();
		
		return v;
	}

	public View getContentView() {
		return contentViewReference.get();
	}

	public Context getContext() {
		return getActivity().getApplicationContext();
	}

	public void unregisterReceiver(BroadcastReceiver receiver) {
		getActivity().unregisterReceiver(receiver);
	}

	public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
		getActivity().registerReceiver(receiver, filter);
	}

	public void sendBroadcast(Intent intent) {
		getContext().sendBroadcast(intent);
	}

	/**
	 * 通过资源号在界面中查找View。容器是{@link #getLayout()} 中指定的布局。
	 * 
	 * @param id
	 * @return
	 */
	public View findViewById(int id) {
		View v = viewFounds.get(id) != null ? viewFounds.get(id).get() : null;
		if (v == null) {
			v = getContentView() == null ? null : getContentView()
					.findViewById(id);
			if (v != null) {
				viewFounds.put(id, new WeakReference<View>(v));
			}
		}

		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		onCreated(savedInstanceState);
		onPostCreated(savedInstanceState);
		created = true;
	}
	
	protected void callSuperActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	
	public boolean isCreated() {
		return created;
	}
	private boolean foreground = false;

	public boolean isForeground() {
		return foreground;
	}

	public void setForeground(boolean foreground) {
		this.foreground = foreground;
	}
	
	public void toast(int resId) {
		if (isForeground()) {
			Toaster.show(resId);
		}
	}

	public void toast(String message) {
		if (isForeground()) {
			Toaster.show(message);
		}
	}

	public void toastInvalidate(String message) {
		if (isForeground()) {
			Toaster.showInvalidate(message);
		}
	}

	public void toastInvalidate(int resId) {
		if (isForeground()) {
			Toaster.showInvalidate(resId);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		foreground = false;
	}

	public void onResume() {
		super.onResume();
		foreground = true;
	};

	/**
	 * 界面被创建之后调用
	 * 
	 * @param savedInstanceState
	 */
	protected void onPostCreated(Bundle savedInstanceState) {
		// null
	}

	/**
	 * Activity被创建的时候调用
	 * 
	 * @param savedInstanceState
	 */
	protected abstract void onCreated(Bundle savedInstanceState);

	/**
	 * 在此方法返回布局文件的资源号 R.layout.xxx
	 * 
	 * @return
	 */
	protected abstract int getLayout();

	public ActionBar getActionBar() {
		return null;
	}
	
	protected BaseActivity getParentActivity() {
		return (BaseActivity) getActivity();
	}
}
